package com.acwing.partition43;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author rkc
 * @date 2022/7/14 9:23
 */
public class AC4279笛卡尔树 {

    private static final int N = 40;
    private static int[] p = new int[N];
    private static Map<Integer, List<Integer>> map = new HashMap<>(N);
    private static int n, maxd = 0;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = nextInt();
        for (int i = 0; i < n; i++) p[i] = nextInt();
        dfs(0, n - 1, 0);
        for (int i = 0; i <= maxd; i++) {
            for (Integer item : map.get(i)) {
                writer.write(item + " ");
            }
        }
        writer.flush();
    }

    private static void dfs(int l, int r, int d) {
        if (l > r) return;
        maxd = Math.max(maxd, d);
        int root = get(l, r);
        List<Integer> ls = map.getOrDefault(d, new ArrayList<>());
        ls.add(p[root]);
        map.put(d, ls);
        dfs(l, root - 1, d + 1);
        dfs(root + 1, r, d + 1);
    }

    private static int get(int l, int r) {
        int res = l;
        for (int i = l + 1; i <= r; i++) {
            if (p[res] > p[i]) res = i;
        }
        return res;
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
